home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’91 / MS Works Merge Enhancer / THyperXcmd.cp < prev    next >
Encoding:
Text File  |  1991-02-26  |  11.2 KB  |  424 lines  |  [TEXT/MPS ]

  1. /*
  2. Modified from HyperXcmd.glue.inc by
  3. Jeff E Mandel MD MS
  4. Department of Anesthesiology
  5. Tulane University School of Medicine
  6. 1430 Tulane Ave.
  7. New Orleans, LA 70112
  8.  
  9.     THyperXCmd.h
  10.       portions ©Apple Computer, Inc. 1987
  11.     All Rights Reserved.
  12.  
  13. */
  14.  
  15. #include    <THyperXCmd.h>
  16.  
  17. #include    <memory.h>
  18. #include     <String.h>
  19. #include     <Strings.h>
  20. #include     <Errors.h>
  21.  
  22. pascal long TXCMDBlock::StringLength(StringPtr strPtr)
  23. /* Count the characters from where strPtr points until the next zero byte. 
  24.    Does not count the zero itself.  strPtr must be a zero-terminated string. */
  25. {
  26.     inArgs[0] = (long)strPtr;
  27.     request = xreqStringLength;
  28.     ((MyProcPtr) entryPoint)();
  29.     return (long) outArgs[0];
  30. }
  31.  
  32. pascal void TXCMDBlock::SendCardMessage(StringPtr msg)
  33.     /* Send a HyperCard message (a command with arguments) to the current card.
  34.        msg is a pointer to a Pascal format string.  */
  35. {
  36.     inArgs[0] = (long)msg;
  37.     request = xreqSendCardMessage;
  38.     ((MyProcPtr) entryPoint)();
  39. }
  40.  
  41. pascal Handle TXCMDBlock::EvalExpr(StringPtr expr)
  42.     /* Evaluate a HyperCard expression and return the answer.  The answer is
  43.        a handle to a zero-terminated string. */
  44. {
  45.     inArgs[0] = (long)expr;
  46.     request = xreqEvalExpr;
  47.     ((MyProcPtr) entryPoint)();
  48.     return (Handle)(outArgs[0]);
  49. }
  50.  
  51. pascal void TXCMDBlock::ZeroToPas(char *zeroStr, StringPtr pasStr)
  52. {
  53.     inArgs[0] = (long)zeroStr;
  54.     inArgs[1] = (long)pasStr;
  55.     request = xreqZeroToPas;
  56.     ((MyProcPtr) entryPoint)();
  57. }
  58.  
  59. pascal Handle TXCMDBlock::PasToZero(StringPtr pasStr)
  60. {
  61.     inArgs[0] = (long)pasStr;
  62.     request = xreqPasToZero;
  63.     ((MyProcPtr) entryPoint)();
  64.     return (Handle) outArgs[0];
  65. }
  66.  
  67. pascal void TXCMDBlock::SendHCMessage(StringPtr msg)
  68.     /* Send a HyperCard message (a command with arguments) to HyperCard.
  69.        msg is a pointer to a Pascal format string.  */
  70. {
  71.     inArgs[0] = (long)msg;
  72.     request = xreqSendHCMessage;
  73.     ((MyProcPtr) entryPoint)();
  74. }
  75.  
  76. pascal Handle TXCMDBlock::GetGlobal(StringPtr globName)
  77. /* Return a handle to a zero-terminated string containing the value of 
  78.    the specified HyperTalk global variable. */
  79. {
  80.     inArgs[0] = (long)globName;
  81.     request = xreqGetGlobal;
  82.     ((MyProcPtr) entryPoint)();
  83.     return (Handle)(outArgs[0]);
  84. }
  85.  
  86. pascal void TXCMDBlock::SetGlobal(StringPtr globName, Handle globValue)
  87. /* Set the value of the specified HyperTalk global variable to be
  88.    the zero-terminated string in globValue.  The contents of the 
  89.    Handle are copied, so you must still dispose it afterwards.  */
  90. {
  91.     inArgs[0] = (long)globName;
  92.     inArgs[1] = (long)globValue;
  93.     request = xreqSetGlobal;
  94.     ((MyProcPtr) entryPoint)();
  95. }
  96.  
  97. pascal Handle TXCMDBlock::GetFieldByName(Boolean cardFieldFlag, StringPtr fieldName)
  98. /* Return a handle to a zero-terminated string containing the value of 
  99.    field fieldName on the current card.  You must dispose the handle. */
  100. {
  101.     inArgs[0] = (long)cardFieldFlag;
  102.     inArgs[1] = (long)fieldName;
  103.     request = xreqGetFieldByName;
  104.     ((MyProcPtr) entryPoint)();
  105.     return (Handle)(outArgs[0]);
  106. }
  107.  
  108. pascal Handle TXCMDBlock::GetFieldByNameC(Boolean cardFieldFlag, CString fieldName)
  109. /* Return a handle to a zero-terminated string containing the value of 
  110.    field fieldName on the current card.  You must dispose the handle. */
  111. {
  112.     StringPtr p=(StringPtr) fieldName;
  113.     inArgs[0] = (long)cardFieldFlag;
  114.     inArgs[1] = (long)p;
  115.     request = xreqGetFieldByName;
  116.     ((MyProcPtr) entryPoint)();
  117.     return (Handle)(outArgs[0]);
  118. }
  119.  
  120. pascal void TXCMDBlock::SetFieldByNameC(Boolean cardFieldFlag, CString fieldName,
  121.     Handle fieldVal)
  122. /* Set the value of field fieldName to be the zero-terminated string 
  123.    in fieldVal.  The contents of the Handle are copied, so you must 
  124.    still dispose it afterwards. */
  125. {
  126.     StringPtr p=(StringPtr) fieldName;
  127.     inArgs[0] = (long)cardFieldFlag;
  128.     inArgs[1] = (long)p;
  129.     inArgs[2] = (long)fieldVal;
  130.     request = xreqSetFieldByName;
  131.     ((MyProcPtr) entryPoint)();
  132.     DisposHandle(fieldVal);
  133. }
  134.  
  135. pascal Handle TXCMDBlock::GetFieldByNum(Boolean cardFieldFlag, short fieldNum)
  136. /* Return a handle to a zero-terminated string containing the value of 
  137.    field fieldNum on the current card.  You must dispose the handle. */
  138. {
  139.     inArgs[0] = (long)cardFieldFlag;
  140.     inArgs[1] = fieldNum;
  141.     request = xreqGetFieldByNum;
  142.     ((MyProcPtr) entryPoint)();
  143.     return (Handle)(outArgs[0]);
  144. }
  145.  
  146. pascal Handle TXCMDBlock::GetFieldByID(Boolean cardFieldFlag, short fieldID)
  147. /* Return a handle to a zero-terminated string containing the value of 
  148.    the field whise ID is fieldID.  You must dispose the handle. */
  149. {
  150.     inArgs[0] = (long)cardFieldFlag;
  151.     inArgs[1] = fieldID;
  152.     request = xreqGetFieldByID;
  153.     ((MyProcPtr) entryPoint)();
  154.     return (Handle)(outArgs[0]);
  155. }
  156.  
  157. pascal void TXCMDBlock::SetFieldByName(Boolean cardFieldFlag, StringPtr fieldName,
  158.     Handle fieldVal)
  159. /* Set the value of field fieldName to be the zero-terminated string 
  160.    in fieldVal.  The contents of the Handle are copied, so you must 
  161.    still dispose it afterwards. */
  162. {
  163.     inArgs[0] = (long)cardFieldFlag;
  164.     inArgs[1] = (long)fieldName;
  165.     inArgs[2] = (long)fieldVal;
  166.     request = xreqSetFieldByName;
  167.     ((MyProcPtr) entryPoint)();
  168.     DisposHandle(fieldVal);
  169. }
  170.  
  171. pascal void TXCMDBlock::SetFieldByNum(Boolean cardFieldFlag, short fieldNum,
  172.     Handle fieldVal)
  173. /* Set the value of field fieldNum to be the zero-terminated string 
  174.    in fieldVal.  The contents of the Handle are copied, so you must 
  175.    still dispose it afterwards. */
  176. {
  177.     inArgs[0] = (long)cardFieldFlag;
  178.     inArgs[1] = fieldNum;
  179.     inArgs[2] = (long)fieldVal;
  180.     request = xreqSetFieldByNum;
  181.     ((MyProcPtr) entryPoint)();
  182.     DisposHandle(fieldVal);
  183. }
  184.  
  185. pascal void TXCMDBlock::SetFieldByID(Boolean cardFieldFlag, short fieldID,
  186.     Handle fieldVal)
  187. /* Set the value of the field whose ID is fieldID to be the zero-
  188.    terminated string in fieldVal.  The contents of the Handle are 
  189.    copied, so you must still dispose it afterwards. */
  190. {
  191.     inArgs[0] = (long)cardFieldFlag;
  192.     inArgs[1] = fieldID;
  193.     inArgs[2] = (long)fieldVal;
  194.     request = xreqSetFieldByID;
  195.     ((MyProcPtr) entryPoint)();
  196.     DisposHandle(fieldVal);
  197. }
  198.  
  199. pascal Boolean TXCMDBlock::StrToBool(char *str)
  200. /* Convert the Pascal strings 'true' and 'false' to booleans. */
  201. {
  202.     inArgs[0] = (long) str;
  203.     request = xreqStrToBool;
  204.     ((MyProcPtr) entryPoint)();
  205.     return (Boolean) outArgs[0];
  206. }
  207.  
  208. pascal long TXCMDBlock::StrToLong(char *str)
  209. /* Convert a string of ASCII decimal digits to an unsigned long integer. */
  210. {
  211.     inArgs[0] = (long)str;
  212.     request = xreqStrToLong;
  213.     ((MyProcPtr) entryPoint)();
  214.     return (long) outArgs[0];
  215. }
  216.  
  217. pascal void TXCMDBlock::LongToStr(long num,StringPtr mystr)
  218. {
  219.     inArgs[0] = num;
  220.     inArgs[1] = (long)mystr;
  221.     request = xreqLongToStr;
  222.     ((MyProcPtr) (entryPoint))();
  223. }
  224.  
  225. pascal void TXCMDBlock::SendCardMessageC(CString msg)
  226.     /* Send a HyperCard message (a command with arguments) to the current card.
  227.        msg is a CString.  */
  228. {
  229.     char *p=(char *)msg;
  230.     c2pstr(p);
  231.     inArgs[0] = (long)p;
  232.     request = xreqSendCardMessage;
  233.     ((MyProcPtr) entryPoint)();
  234. }
  235.  
  236. void TXCMDBlock::SignalFatalError(char *msg)
  237. {
  238.     
  239.     CString theMessage((char *)"answer \"");
  240.     theMessage += CString (msg) + CString("\"");
  241.     SendCardMessageC(theMessage);
  242.     SignalReturnStatus("\p#@!%");
  243. }
  244.  
  245. void TXCMDBlock::SignalFatalError(CString msg)
  246. {
  247.     
  248.     CString theMessage((char *)"answer \"");
  249.     theMessage += msg + CString("\"");
  250.     SendCardMessageC(theMessage);
  251.     SignalReturnStatus("\p#@!%");
  252. }
  253.  
  254. void TXCMDBlock::SignalReturnStatus (StringPtr theMessage)
  255. {
  256.     returnValue = PasToZero(theMessage);
  257.     return;
  258. }
  259.  
  260. void TXCMDBlock::SignalReturnStatus (Handle theMessage)
  261. {
  262.     returnValue = theMessage;
  263.     return;
  264. }
  265.  
  266. void TXCMDBlock::SignalReturnStatus (Ptr theMessage)
  267. {
  268.     StringPtr p = (StringPtr) new char[256];
  269.     strcpy((Ptr) p, theMessage);
  270.     c2pstr((Ptr) p);
  271.     returnValue = PasToZero(p);
  272.     delete p;
  273.     return;
  274. }
  275.  
  276. void TXCMDBlock::SignalReturnStatus (CString theMessage)
  277. {
  278.     returnValue = (Handle) theMessage;
  279.     return;
  280. }
  281.  
  282. Boolean TXCMDBlock::informArgNums(char *theXCMD, short expected)
  283. {
  284.     
  285.     
  286.     if (paramCount==expected)
  287.         return(true);
  288.     else
  289.     {
  290.         CString theMessage((char *)"answer \"");
  291.         theMessage += CString(theXCMD) + CString((char *)" requires ") 
  292.                     + CString ((long) expected);
  293.         if (expected==1)
  294.             theMessage += CString((char *)" argument.\" ");
  295.         else
  296.             theMessage += CString((char *)" arguments.\" ");
  297.         
  298.         SendCardMessageC (theMessage);
  299.         SignalReturnStatus("\p#@!%");
  300.         return(false);
  301.     }
  302. }
  303.  
  304. Boolean TXCMDBlock::RecoverBooleanArg(short theNum)
  305. {
  306.     HLock(params[theNum]);
  307.     Ptr temp = new char[64];
  308.     strcpy(temp, *params[theNum]);
  309.     c2pstr(temp);
  310.     Boolean theResult = StrToBool(temp);
  311.     HUnlock(params[theNum]);
  312.     delete temp;
  313.     return theResult;
  314. }
  315.  
  316. Ptr TXCMDBlock::RecoverStringArg(short theNum)
  317. {
  318.     HLock(params[theNum]);
  319.     Ptr result = new char[(Size) strlen(*params[theNum])];
  320.     strcpy(result, *params[theNum]);
  321.     HUnlock(params[theNum]);
  322.     return result;
  323. }
  324.  
  325. void TXCMDBlock::CopyStringArg(short theNum, StringPtr theString)
  326. {
  327.     HLock(params[theNum]);
  328.     strcpy((char *) theString, *params[theNum]);
  329.     c2pstr((char *) theString);
  330.     HUnlock(params[theNum]);
  331. }
  332.  
  333. void TXCMDBlock::CopyStringArg(short theNum, Ptr theString)
  334. {
  335.     HLock(params[theNum]);
  336.     strcpy(theString, *params[theNum]);
  337.     HUnlock(params[theNum]);
  338. }
  339.  
  340. CString TXCMDBlock::CStringArg(short theNum)
  341. {
  342.     HLock(params[theNum]);
  343.     CString theResult = CString( *params[theNum]);
  344.     HUnlock(params[theNum]);
  345.     return theResult;
  346. }
  347.  
  348. long TXCMDBlock::RecoverLongArg(short theNum)
  349. {
  350.     HLock(params[theNum]);
  351.     Ptr temp = new char[64];
  352.     strcpy(temp, *params[theNum]);
  353.     c2pstr(temp);
  354.     long theResult = StrToLong(temp);
  355.     HUnlock(params[theNum]);
  356.     delete temp;
  357.     return theResult;
  358. }
  359.  
  360. long TXCMDBlock::RecoverLongGlobal(StringPtr globName)
  361. {
  362.     Handle theString=GetGlobal(globName);
  363.     if (strlen(*theString))
  364.     {
  365.         HLock(theString);
  366.         StringPtr zeroStr= (StringPtr) new char[256];
  367.         ZeroToPas(*theString, zeroStr);
  368.         long result=StrToLong((char *) zeroStr);
  369.         DisposHandle(theString);
  370.         delete zeroStr;
  371.         return (result);
  372.     }
  373.     else
  374.         return 0;
  375.     
  376. }
  377.  
  378. void TXCMDBlock::SetLongGlobal(StringPtr globName, long theVal)
  379. {
  380.     StringPtr tempStringPtr= (StringPtr) new char[256];
  381.     LongToStr(theVal, tempStringPtr);
  382.     Handle theString= PasToZero(tempStringPtr);
  383.     SetGlobal(globName, theString);
  384.     delete tempStringPtr;
  385.     DisposHandle(theString);
  386.     return;
  387. }
  388.  
  389. void TXCMDBlock::SignalFileErr(OSErr theErr)
  390. {
  391.     CString temp;
  392.     switch (theErr)
  393.     {
  394.         case fnfErr:    returnValue = (Handle)NewString("\pFile not found!");
  395.                         break;
  396.         case bdNamErr:    returnValue = (Handle)NewString("\pFile name bad!");
  397.                         break;
  398.         case dupFNErr:    returnValue = (Handle)NewString("\pFile already exists!");
  399.                         break;
  400.         case ioErr:        returnValue = (Handle)NewString("\pFile IO error!");
  401.                         break;
  402.         case nsvErr:    returnValue = (Handle)NewString("\pFile volume does not exist!");
  403.                         break;
  404.         case extFSErr:    returnValue = (Handle)NewString("\pFile system error!");
  405.                         break;
  406.         case rfNumErr:    returnValue = (Handle)NewString("\pFile refNum bad!");
  407.                         break;
  408.         case fnOpnErr:    returnValue = (Handle)NewString("\pFile not open!");
  409.                         break;
  410.         case paramErr:    returnValue = (Handle)NewString("\pFile parameter error!");
  411.                         break;
  412.         case eofErr:    returnValue = (Handle)NewString("\pFile end encountered!");
  413.                         break;
  414.         case resNotFound:    returnValue = (Handle)NewString("\pFile does not contain resource!");
  415.                         break;
  416.         default:        temp = CString("Unknown file err (") + CString((long)theErr) + CString(")");
  417.                         returnValue = (Handle)NewString((StringPtr)temp);
  418.                         break;
  419.     }
  420.     HLock(returnValue);
  421.     p2cstr((StringPtr) *returnValue);
  422.     HUnlock(returnValue);
  423.     return;
  424. }